<?php

class Rcp_Methods_Data_ApplicationRequest {
	
	/**
	 * 
	 * @var RcpEventRequestTypes
	 */
	private $_eventRequestType = null;
	
	public $code;
	public $corporateOutput;
	/**
	 * 
	 * @var Rcp_Methods_Data_DateRange
	 */
	public $range;

	public function __construct($data) {
		$this->code = $data->code;
		$this->corporateOutput = (bool)$data->corporateOutput;
		$this->range = new Rcp_Methods_Data_DateRange($data->range);
	}
	
	/**
	 * @throws Rcp_Exception_App
	 * @return RcpEventRequestTypes
	 */
	public function getEventRequestType() {
		if ($this->_eventRequestType === null) {
			$this->_eventRequestType = RcpEventRequestTypesQuery::create()->findByCode($this->code);
			if (!($this->_eventRequestType instanceof RcpEventRequestTypes))
				throw new Rcp_Exception_App(Rcp_Exception_Codes::ERROR_EVENT_REQUEST_CODE_NOT_EXIST, array($this->code), array('code' => $this->code));
		}
		return $this->_eventRequestType;
	}
	
	public function createEventRequest($calendarEventId = null) {		
		$eventRequestCalendarEvent = new RcpEventRequestHasCalendarEvent();
		$eventRequestCalendarEvent->setCalendarEventId($calendarEventId);
		$eventRequestCalendarEvent->setStartDate($this->range->from);
		$eventRequestCalendarEvent->setEndDate($this->range->to);
		$eventRequestCalendarEvent->setActive(true);
		
		$eventRequest = new RcpEventRequests();
		$eventRequest->setStatus(RcpEventRequests::STATUS_NEW);
		$eventRequest->setCorporateOutput($this->corporateOutput);
		$eventRequest->setEventRequestTypes($this->getEventRequestType());
		$eventRequest->addEventRequestHasCalendarEvent($eventRequestCalendarEvent);
	}
	
}